﻿using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using UnityEditor;
using UnityEngine;
using Process = System.Diagnostics.Process;
[CustomEditor(typeof(UpdateSettings))]
public class UpdateSettingsInspector : Editor
{
    //private UpdateSettings m_updateSettings;
    GameSetting GameSettingInspector;
    private void Awake()
    {
        //m_updateSettings = target as UpdateSettings;

        GameSettingInspector = LoadSetting();

        //Debug.Log($"UpdateSettingsInspector load: {GameSettingInspector.ResType}\nURL: {GameSettingInspector.UpdateHost}\nServerHost: {GameSettingInspector.ServerHost}");
    }

    public override void OnInspectorGUI()
    {
        ////加入此句，不影响原在Inspector绘制的元素
        //base.OnInspectorGUI();

        //输入框控件
        GameSettingInspector.UpdateHost = EditorGUILayout.TextField("更新地址", GameSettingInspector.UpdateHost);
        GameSettingInspector.ServerHost = EditorGUILayout.TextField("服务器地址", GameSettingInspector.ServerHost);
        if (GUILayout.Button("正式服 地址为空"))
        {
            GameSettingInspector.ServerHost = string.Empty;
        }
        if (GUILayout.Button("外网测试服"))
        {
            GameSettingInspector.ServerHost = "106.75.105.246 6810";
        }
        if (GUILayout.Button("内网"))
        {
            GameSettingInspector.ServerHost = "10.10.1.69 6810";
        }
        if (GUILayout.Button("hybery"))
        {
            GameSettingInspector.ServerHost = "10.10.2.95 9400";
        }
        if (GUILayout.Button("leo"))
        {
            GameSettingInspector.ServerHost = "10.10.2.37 9300";
        }
        if (GUILayout.Button("mingo"))
        {
            GameSettingInspector.ServerHost = "10.10.2.80 9300";
        }
        if (GUILayout.Button("hunk"))
        {
            GameSettingInspector.ServerHost = "10.10.2.212 9400";
        }
        if (GUILayout.Button("jeffrey"))
        {
            GameSettingInspector.ServerHost = "10.10.2.243 9300";
        }
        if (GUILayout.Button("cai"))
        {
            GameSettingInspector.ServerHost = "10.10.2.175 9400";
        }
        if (GUILayout.Button("leon"))
        {
            GameSettingInspector.ServerHost = "10.10.2.38 10400";
        }

        GameSettingInspector.gameUrlVersion = EditorGUILayout.IntField("gameUrlVersion", GameSettingInspector.gameUrlVersion);
        GameSettingInspector.appKey = EditorGUILayout.TextField("appKey", GameSettingInspector.appKey);
        GameSettingInspector.appId = EditorGUILayout.TextField("appId", GameSettingInspector.appId);
        GameSettingInspector.gameId = EditorGUILayout.IntField("gameId", GameSettingInspector.gameId);
        GameSettingInspector.domain = EditorGUILayout.TextField("domain", GameSettingInspector.domain);
        GameSettingInspector.channel = EditorGUILayout.TextField("channel", GameSettingInspector.channel);
        GameSettingInspector.gameVersion = EditorGUILayout.IntField("gameVersion", GameSettingInspector.gameVersion);

        GameSettingInspector.emResType = (ResourceMode)EditorGUILayout.EnumPopup("资源模式:", GameSettingInspector.emResType);

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("保存", GUILayout.Width(200)))
        {
            Debug.Log($"UpdateSettingsInspector load: {GameSettingInspector.ResType}\nURL: {GameSettingInspector.UpdateHost}\nServerHost: {GameSettingInspector.ServerHost}");
            SaveSetting(GameSettingInspector);
        }
        if (GUILayout.Button("复制文件", GUILayout.Width(200)))
        {
            SaveSetting(GameSettingInspector);
            StringBuilder stringBuilder = new StringBuilder("Set-Clipboard -Path ");
            stringBuilder.Append(Application.streamingAssetsPath + "/Dev_Setting.xml");
            RunCommand(stringBuilder.Replace("/", "\\").ToString());
            Object obj = AssetDatabase.LoadAssetAtPath<Object>("Assets/StreamingAssets/Dev_Setting.xml");
            EditorGUIUtility.PingObject(obj);
        }
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("本地目录", GUILayout.Width(200)))
        {
            string path = Application.persistentDataPath;
            path = path.Replace("/", "\\");
            Process.Start("explorer", path);
        }

        if (GUILayout.Button("30009", GUILayout.Width(100)))
        {
            GameSettingInspector.gameUrlVersion = 100042;
            GameSettingInspector.appKey = "7406bd04be85784c74bd8e0a1fbd83b2";
            GameSettingInspector.appId = "30009";
            GameSettingInspector.gameId = 30009;
            GameSettingInspector.domain = "365you.com";
            GameSettingInspector.channel = "365you.dyzbcyddz.5006002093";
            GameSettingInspector.emResType = ResourceMode.UpdateableBundle;
            SaveSetting(GameSettingInspector);
        }
        if (GUILayout.Button("35119", GUILayout.Width(100)))
        {
            GameSettingInspector.gameUrlVersion = 100042;
            GameSettingInspector.appKey = "af45a604ac8d5960b51e8332992bba15";
            GameSettingInspector.appId = "35119";
            GameSettingInspector.gameId = 35119;
            GameSettingInspector.domain = "365you.com";
            GameSettingInspector.channel = "365you.dyzbcyddz.202120212021";
            GameSettingInspector.emResType = ResourceMode.UpdateableBundle;
            SaveSetting(GameSettingInspector);
        }
        if (GUILayout.Button("ios", GUILayout.Width(100)))
        {
            GameSettingInspector.gameUrlVersion = 101023;
            GameSettingInspector.appKey = "7406bd04be85784c74bd8e0a1fbd83b2";
            GameSettingInspector.appId = "30009";
            GameSettingInspector.gameId = 30009;
            GameSettingInspector.domain = "mmnow.cn";
            GameSettingInspector.channel = "ios.cyddz";
            GameSettingInspector.emResType = ResourceMode.UpdateableBundle;
            SaveSetting(GameSettingInspector);
        }
        GUILayout.EndHorizontal();
    }

    public static bool RunCommand(string command)
    {
        using (Process process = new Process())
        {
            process.StartInfo.FileName = "powershell";

            process.StartInfo.Arguments = command;

            process.StartInfo.CreateNoWindow = true; // 不显示窗口

            process.StartInfo.ErrorDialog = true;

            process.StartInfo.UseShellExecute = false;

            try

            {
                process.Start();

                process.WaitForExit();

                process.Close();

            }

            catch (System.Exception e)

            {
                Debug.LogError(e);

                return false;

            }

        }

        return true;

    }

    //设置写入xml
    private void WriteXml<T>(string path, T obj)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        XmlTextWriter tw = new XmlTextWriter(path, System.Text.Encoding.UTF8);
        tw.Formatting = System.Xml.Formatting.Indented;
        serializer.Serialize(tw, obj);
        XmlDocument doc = new XmlDocument();
        doc.Save(tw);
        tw.Close();
    }

    public void SaveSetting(GameSetting setting)
    {
        string settingFile = UpdateSettings.SettingFilePath();
        WriteXml(settingFile, setting);
        AssetDatabase.Refresh();
    }

    public GameSetting LoadSetting()
    {
        string settingFile = UpdateSettings.SettingFilePath();
        GameSetting setting = new GameSetting();
        if (System.IO.File.Exists(settingFile))
        {
            setting = UpdateSettings.ReadXml<GameSetting>(File.ReadAllText(settingFile));
        }
        else
        {
            setting.UpdateHost = null;
            setting.ServerHost = null;
            setting.ResType = (int)ResourceMode.EditorLocalRes;
        }
        return setting;
    }
}
